package com.zzh.takeoutapp.activity;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.utils.SPUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;

import net.arvin.pictureselector.entities.ImageEntity;
import net.arvin.pictureselector.utils.PSConfigUtil;
import net.arvin.pictureselector.utils.PSConstanceUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class HeadPortraitActivity extends BaseActivity {
    @Bind(R.id.rl_face)
    RelativeLayout rlFace;
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.iv_face)
    ImageView iv_face;
    @Bind(R.id.tv_nickname)
    TextView tv_nickname;
    @Bind(R.id.tv_mobile)
    TextView tv_mobile;
    @Bind(R.id.comment_username)
    RelativeLayout comment_username;
    private final int REQUEST_CODE_1 = 1001;
    private final int REQUEST_CODE_2 = 1002;
    private ArrayList<ImageEntity> mItems;//用于显示的图集
    private final String HEAD_PORTRAIT = "HeadPortraitActivity";
    private final String MINE_FRAGMENT = "MineFragment";

    @Override
    public void setContent() {
        setContentView(R.layout.activity_head_portrait);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        PSConfigUtil.getInstance()
                .setCanCrop(true)
                .setCanTakePhoto(true)
                .setMaxCount(1)
                .setStatusBarColor(R.color.blue);
        mItems = new ArrayList<>();
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "我的账号");
        ImageManager.loadUrlImage(this, getIntent().getStringExtra("face"), iv_face);
        tv_nickname.setText(getIntent().getStringExtra("nickname"));
        tv_mobile.setText(getIntent().getStringExtra("mobile"));
    }

    @Override
    public void setListener() {

    }

    private void openPs() {
        String perms[] = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        checkPermission(new CheckPermListener() {
            @Override
            public void agreeAllPermission() {
                PSConfigUtil.getInstance().showSelector(HeadPortraitActivity.this, REQUEST_CODE_1, mItems);
            }
        }, "获取相片需要使用相机和内存读写权限", perms);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_2 && data != null) {
            String nickname = data.getStringExtra("nickname");
            if (nickname != null)
                tv_nickname.setText(nickname);
        }
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case REQUEST_CODE_1:
                    getHeadPortrait(data);
                    break;
            }
        }
    }

    private void getHeadPortrait(Intent data) {
        mItems.clear();
        List<ImageEntity> temp = data.getParcelableArrayListExtra(PSConstanceUtil.PASS_SELECTED);
        mItems.addAll(temp);
        List<File> strings = new ArrayList<>();
        for (ImageEntity entity : temp) {
            strings.add(new File(entity.getPath()));
        }
        ServiceUrl.UpdaFaceService(this, tv_nickname.getText().toString(), strings, HEAD_PORTRAIT);

    }

    @Override
    protected void backFromSetting() {
        super.backFromSetting();
        openPs();
    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }


    @Override
    public void onStop() {
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case HEAD_PORTRAIT:
                if (jsonObject != null) {
                    if (jsonObject.optString("message").equals("success")) {
                        JSONObject object = jsonObject.optJSONObject("data");
                        ImageManager.loadUrlImage(this, object.optString("face"), iv_face);
                        ServiceUrl.UserInfoService(this, MINE_FRAGMENT);
                    }
                }
                break;
        }
    }

    @OnClick({R.id.rl_face, R.id.comment_username})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rl_face:
                openPs();
                break;
            case R.id.comment_username:
                start_ActivityForResult(new Intent(this, AlterUserNaneActivity.class), REQUEST_CODE_2);
                break;
        }
    }
}
